
#include "advancedMovieItem.h"

	advancedMovieItem::advancedMovieItem (){
	    super::ofVideoPlayer();
	    bAnimate = false;
	    bShrinkEnd = false;
	    bMousePressed= false;
	    dLeftDoublepressed = false;
	    bShowSliderShrink = false;
	    bShowSliderRotateZ = false;
	    bShowSliderTimeLine = false;
	    bSlidePauseState = false;
	    shrinkFactor = 1;//0.5;//
	    rotateZ = ofRandom(0, 360);
	    shiftX = 0;
	    shiftY = 0;
        mouseShiftX = 0;
	    mouseShiftY = 0;
	    polaroid.loadImage("movies/polaroid.jpg");
	    timer = 0;

	    maxShrink = 0.4;//0.2;//
	    minShrink = 2.0;//1.0;//
	    width = width/2;
	    height = height/2;
	}

	void advancedMovieItem::animate(bool bAnimate){
        this->bAnimate = bAnimate;
	}

	void advancedMovieItem::shrink(float factor){
	    if(shrinkFactor-factor >= maxShrink && shrinkFactor- factor - minShrink <= 0){
            shrinkFactor-= factor;
	    }else{
	        bShrinkEnd = true;
            setPaused(true);
        }
	}

    void advancedMovieItem::rotate(float x, float y, float z){
        rotateZ += z;
            while(rotateZ > 360){
                    rotateZ -= 360;
            }
            while(rotateZ < 0){
                rotateZ += 360;
            }
    }

	void advancedMovieItem::draw(float x, float y){
	 //  this->x = x + mouseShiftX;
	 //   this->y = y + mouseShiftY;
	    move(   (x+mouseShiftX) - this->x,
                (y+mouseShiftY) - this->y);
        x = this->x;
        y = this->y;

	     ofPushMatrix();

	     	    if(bAnimate && !bShrinkEnd){
                    shrink(0.001);

                    //update Rotate variable
                    rotate(0,0,1);

                    //Move along a path
                    motionPath();
                }
                  //  ofTranslate(mouseShiftX, mouseShiftY, 0);

            //rotate
            ofTranslate(x+width*shrinkFactor/2, y+height*shrinkFactor/2, 0);
                ofRotateZ(rotateZ);
            ofTranslate(-(x+width*shrinkFactor/2), -(y+height*shrinkFactor/2), 0);

            polaroid.draw(x,y, polaroid.width*shrinkFactor, polaroid.height*shrinkFactor);
            super::draw(x+22*shrinkFactor,y+42*shrinkFactor,  width*shrinkFactor, height*shrinkFactor);
           ofPopMatrix();

           showSliderShrink();
           showSliderRotateZ();
           showSliderTimeLine();
	}

    void advancedMovieItem::motionPath(){
        shiftX = ofRandom(shiftX-1,shiftX+1);
        shiftY = ofRandom(shiftY-1,shiftY+1);
        ofTranslate(shiftX, shiftY, 0);
    }

    bool advancedMovieItem::togglePause(){
        if(bPaused){
            setSpeed(1);
        }else{
            setSpeed(0);
        }
        bPaused = !bPaused;

        return bPaused;
    }

    void advancedMovieItem::setPaused(bool _bPaused){
        bPaused = !_bPaused;
        togglePause();
    }

   void advancedMovieItem::showSliderShrink(){
       if(bShowSliderShrink){
           ofSetColor(0xFFFFFFFF);
            ofSetLineWidth(10) ;
            float percentage = (shrinkFactor-maxShrink)/(minShrink-maxShrink);

           ofLine(mouseStartX-5,mouseStartY + 200*(1-percentage), mouseStartX-5, mouseStartY- 200*percentage);
           ofDrawBitmapString("shrink "+ofToString((int) (100*percentage))+"%",mouseStartX - 100, mouseStartY - 15);

       }else{

       }
   }

   void advancedMovieItem::showSliderRotateZ(){
        if(bShowSliderRotateZ){
           ofSetColor(0xFFFFFFFF);
            ofSetLineWidth(10) ;
            float percentage = rotateZ/360;

           ofLine(mouseStartX+ 200*(1-percentage),mouseStartY - 5, mouseStartX- 200*(percentage), mouseStartY - 5);
           ofDrawBitmapString("rotate "+ofToString((int)(rotateZ))+" degrees",mouseStartX + 10, mouseStartY +10);
       }else{

       }
   }

   void advancedMovieItem::showSliderTimeLine(){
        if(bShowSliderTimeLine){
           ofSetColor(0xFFFFFFFF);
            ofSetLineWidth(10) ;
            float percentage = getPosition();


           ofLine(mouseStartX+ 200*(1-percentage),mouseStartY - 5, mouseStartX- 200*(percentage), mouseStartY - 5);
           ofDrawBitmapString("duration "+ofToString((int)(getDuration()*percentage)/60)+"."+ofToString((int)(getDuration()*percentage)%60)+" min",mouseStartX + 10, mouseStartY +10);
       }else{

       }

   }

   void advancedMovieItem::moveThroughTimeLine(float _position){
      // if(getPosition()+(0.01*_position) <= 1 )){
            setPosition(getPosition()+(0.01*_position));
      // }
   }

   void advancedMovieItem::move(int _x, int _y){
       if(x + _x < ofGetWidth() && x + _x > 0){
            this->x += _x;
       }
        if(y + _y < ofGetHeight() && y + _y > 0){
            this->y += _y;
        }

               if(x > ofGetWidth() ){
            x = ofGetWidth() - 5;
       }else if(x < 0) {
            x = 5;
       }
       if(y > ofGetHeight() ){
            y = ofGetHeight() - 5;
       }else if(y < 0) {
            y = 5;
       }
   }

    bool advancedMovieItem::mousePressed(int x, int y, int button){
        int tx =  x - this->x;
	    int ty =  y - this->y;

	    if(tx < polaroid.width*shrinkFactor && tx > 0 && ty < polaroid.height*shrinkFactor && ty > 0){
            bMousePressed= true;
            mouseStartX = x;
            mouseStartY = y;
            if(button == 0){
                //left mouse
                //timer for faking doubleclick
                float timePressed = ofGetElapsedTimeMillis() ;
                if(timePressed - timer < 500){
                    togglePause();
                }
                timer = timePressed;

                animate(false);
            }else if(button== 2){
                //right mouse
                //timer for faking doubleclick
                float timePressed = ofGetElapsedTimeMillis() ;
                if(timePressed - timer < 500){
                    dLeftDoublepressed = true;
                    bShowSliderRotateZ = true;
                    bShowSliderShrink = true;
                }else{
                     dLeftDoublepressed = false;
                }
                timer = timePressed;

                animate(false);
            }

	    }else{
            bMousePressed= false;
	    }
	    return bMousePressed;
    }

    bool advancedMovieItem::mouseDragged(int x, int y, int button){
        if(bMousePressed){
            if(button == 0){
                bSlidePauseState = bPaused;
                setPaused(true);
                moveThroughTimeLine(x - mouseStartX);
                bShowSliderTimeLine = true;

            }else if(button ==2){
                if(dLeftDoublepressed){
                    rotate(0,0,(mouseStartX - x));
                    shrink((mouseStartY - y)*0.01);

                    bShowSliderShrink = true;
                    bShowSliderRotateZ = true;
                }else{
                    mouseShiftX -= mouseStartX - x;
                    mouseShiftY -= mouseStartY - y;
                    move(mouseShiftX, mouseShiftY);
                }
            }
                mouseStartX = x;
                mouseStartY = y;
        }
        return bMousePressed;
    }

	bool advancedMovieItem::mouseReleased(int x, int y, int button){
	    int tx =  x - this->x;
	    int ty =  y - this->y;

	    if(tx < polaroid.width*shrinkFactor && tx > 0 && ty < polaroid.height*shrinkFactor && ty > 0 && bMousePressed){
            if(bShrinkEnd){

            }else{
                animate(true);
            }
	    }else{
            bMousePressed = false;
	    }

	    if(bShowSliderTimeLine){
            setPaused(bSlidePauseState);
            bShowSliderTimeLine = false;
	    }

	    //unset variables
        dLeftDoublepressed = false;
	    bShowSliderShrink = false;
        bShowSliderRotateZ = false;

        return bMousePressed;
	}


